// pages/courselist/courselist.js
const httpUtils = require('../../utils/httpUtils.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    courses: null,
    total: null,
    showSearchResult: false,
    page: 1,
    id:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id
    })
    this.getData();
  },
  getData: function () {
    let that = this
    wx.showLoading({
      title: '正在加载',
      mask: true
    })
    httpUtils.courselist({
      access_token: app.globalData.$TOKEN,
      type: that.data.id,
      page: that.data.page,
      size: 20,
    }).then((res) => {
      wx.hideLoading()
      if (res.data.length == 0) {
        that.setData({
          showSearchResult: false
        })
      } else {
        console.log("showSearchResult:" + that.data.showSearchResult);
        that.setData({
          showSearchResult: true
        })
      }
      if (that.data.page == 1) {
        that.data.courses = []
        that.data.courses.push(...res.data.lists);
      } else {
        that.data.courses.push(...res.data.lists);
      }
      that.setData({
        courses: that.data.courses,
      });
      that.data.page = that.data.page + 1;

    });

  },
  lookdetail: function (e) {
    var currext = e.currentTarget.dataset['id'];
    console.log("currext:" + currext)
    //查看详情
    wx.navigateTo({
      url: '/pages/coreseDetail/coreseDetail?id=' + currext,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.showSearchResult) {
      this.getData();
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})